package com.peng.leetcode.doublePointer;

/**
 * Main
 *
 * @author: lupeng6
 * @create: 2021/2/2 18:22
 */
public class CharacterReplacement {

    public static void main(String[] args) {
        String s = "AABABBA";
        int k = 1;

        System.out.println(new CharacterReplacement().characterReplacement(s, k));
    }

    public int characterReplacement(String s, int k) {
        int[] charCount = new int[26];
        int length = s.length();
        char[] chars = s.toCharArray();

        int right = 0;
        int left = 0;
        int max = 0;
        int ans = 0;

        while (right < length) {
            char c = chars[right];
            int charIndex = c - 'A';
            charCount[charIndex]++;
            max = Math.max(max, charCount[charIndex]);
            right++;

            if ((right - left - max) > k) {
                charCount[chars[left] - 'A']--;
                left++;
            }
            ans = Math.max(ans, right - left);
        }

        return ans;
    }
}
